<template>
  <div class="home">
    <div class="top">
      <div class="good-list" v-for="item in list" :key="item.id">
        <img :src="item.pic" alt="" />
        <p>{{ item.name }}</p>
        <p>{{item.minPrice}}</p>
        <button @click="add(item)">加入购物车</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
    };
  },
  methods: {
    getlist() {
      fetch("https://api.it120.cc/xiaochengxu/shop/goods/list")
        .then((res) => res.json())
        .then((res) => {
          console.log(res);
          this.list = res.data;
        });
    },
    add(item){
      let {id,pic,minPrice,name} =item
      let obj ={id,pic,minPrice,name}
      this.$store.commit('add',obj)
      this.$router.push('/about')
    }
  },
  created() {
    this.getlist();
  },
};
</script>
<style lang="scss" scoped>
.home {
  .top {
    width: 100%;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    text-align: center;
    align-items: center;
    .good-list {
      width: 33.3%;
      img {
        width: 200px;
        height: 200px;
      }
    }
  }
}
</style>
